package cc.customer.system.service.impl;

import cc.customer.common.service.impl.BaseService;
import cc.customer.system.dao.RoleMapper;
import cc.customer.system.dao.UserRoleMapper;
import cc.customer.system.domain.Role;
import cc.customer.system.domain.RoleWithMenu;
import cc.customer.system.domain.UserRole;
import cc.customer.system.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service("userRoleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserRoleServiceImpl extends BaseService<UserRole> implements UserRoleService {


	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private UserRoleMapper userRoleMapper;


	@Override
	@Transactional
	public void deleteUserRolesByRoleId(String roleIds) {
		List<String> list = Arrays.asList(roleIds.split(","));
		this.batchDelete(list, "roleId", UserRole.class);
	}

	@Override
	@Transactional
	public void deleteUserRolesByUserId(String userIds) {
		List<String> list = Arrays.asList(userIds.split(","));
		this.batchDelete(list, "userId", UserRole.class);
	}
	/**
	 * 取出app用户角色
	 * @param userId
	 * @return
	 */

	@Override
	public String selectRole(Long userId) {
       UserRole userRole = new UserRole();
		userRole.setUserId(userId);
		List<UserRole> userRoleList = userRoleMapper.select(userRole);
		StringBuffer sb = new StringBuffer();
		for (UserRole userRoles : userRoleList) {
			Role role = roleMapper.selectByPrimaryKey(userRoles.getRoleId());
			sb.append(role.getRoleName()).append(",");
		}
		//删除最后一个 ,
		String roles = sb.deleteCharAt(sb.length() - 1).toString();


		return roles;
	}

}
